(function($){
	$.fn.linearContentRelation = function() {


 		return this.each(function() {
 			var select = this;
 			var contentType = $(this).attr('data-content-type');
 			var token = $('#concerto-token').val();
 			var search = $('<input type="text"/>').attr('class', 'multiselect-search');
 			var results = $('<ul/>').attr('class', 'multiselect-search-results').css('display', 'none');
			var ul = $('<ul/>').attr('class', 'multiselect');
			var clearBoth = $('<div/>').css('clear', 'both');
			var createStub = $('<button/>').html('+').attr('disabled', 'disabled').addClass('multiselect-create-stub');
			$(this).after(search);
			search.after(createStub);
			createStub.click(function(e) {
				$(this).attr("disabled", 'disabled');
				e.preventDefault();
				var val = search.val();
				var rootUrl = concerto.rootUrl;
				$.ajax({
					url:rootUrl + '?view=api',
					dataType:'json',
					data:{
						'object':'content',
						'content_type':contentType,
						'content_title':val,
						'content_status':'stub',
						'concerto_token':token
					},
					type:'POST',
					
					success:function(data) {
						var option = $('<option/>').attr('value', data.id).attr('selected', 'selected').html(val).appendTo(select);
						var append = $('<li/>').html(val + '<a class="remove-multiselect" href="javascript:void();">X</a>').appendTo(ul);
						append.data('option', option);
						append.children('.remove-multiselect').click(function() {
							$($(this).parent('li').data('option')).attr('selected', false);
							$(this).parent('li').remove();
						});
						
					}
				});
			});
			createStub.after(clearBoth);
			
			clearBoth.after(results);
			search.attr('placeholder', 'Search...');
			results.after(ul);
			ul.data('select', select);
			results.css('width', search.width() + 'px');
			$(this).children('option:selected').each(function() {
				var child = $('<li/>').data('option', this).html($(this).html() + '<a class="remove-multiselect" href="javascript:void();">X</a>').appendTo(ul);
				child.children('.remove-multiselect').click(function() {
					var parent = $(this).parent('li');
					$(parent.data('option')).attr('selected', false);
		
					parent.remove();
				});
			});
			$(this).hide();	
			search.keyup(function() {
				results.html('');
				var query = $(this).val();
				if(query) {
					createStub.attr('disabled', 'disabled');
					results.show();
					$(select).find(":contains('" + query + "')").not(':selected').each(function() {
						var child = $('<li/>').html($(this).html()).appendTo(results);
						child.data('option', this);
						child.click(function() {
							var append = $('<li/>').html($(this).html() + '<a class="remove-multiselect" href="javascript:void();">X</a>').appendTo(ul);
							append.data('option', $(this).data('option'));
							append.children('.remove-multiselect').click(function() {
								$($(this).parent('li').data('option')).attr('selected', false);
								$(this).parent('li').remove();
							});
							
							$($(this).data('option')).attr('selected', 'selected');
							$(this).remove();
							search.val('');
							results.hide();
						});
					});
					if(!results.children('li').length && !$(select).find(":contains('" + query + "')").length) {
						createStub.attr('disabled', false);
					}
				}
				else {
					createStub.attr('disabled', 'disabled');
					results.hide();
					
				}
			
			});
		});
	};
})(jQuery);


(function($){
	$.fn.triangularContentRelation = function() {
		$(document).ready(function() {
			$('.remove-multiselect').live('click', function() {
				$(this).parent('li').remove();
			});
		});
 		return this.each(function() {
 			var relation = this;
 			var id = $(this).attr('id');
 			// Error box
 			var errorBox = $('<p/>').addClass('relation-error-box').hide();
 			errorBox.appendTo(this);
 			// Create add button
 			var addButton = $('<button/>').html('Add').addClass('relation-add-button').addClass('small-button').attr('disabled', 'disabled').appendTo($(this).find('.inner'));
 			addButton.data('totalAdded', 0);
 			addButton.checkValidity = function() {
 				var valid = true;
				$(relation).find('.multiselect-search').each(function() {
 					if(!$(this).data('value')) {
 						valid = false;
 					}
 				});
 				if(valid) {
 					$(this).attr('disabled', false);
 				}
 				else {
 					$(this).attr('disabled', 'disabled');
 				}

 			};	
 			addButton.click(function(e) {
 				e.preventDefault();
 				var li = $('<li/>');
 				var error = false;
 				var totalAdded = $(this).data('totalAdded');
 				var finds = $(relation).find('.multiselect-search');
 				var totalCount = finds.length;
 				var contentType;
 				var count = 1;
 				finds.each(function() {
 						contentType = $(this).data('content-type');
	 					li.append( 
	 					'<input type="hidden" name="'
	 					+ id + '[' + totalAdded + '][' + contentType + ']" value="'
	 					+ $(this).data('value') 
	 					+ '"/>'
	 					+ '<span class="relation-content">'
	 					+ $(this).val()
	 					+ '</span>');
	 					if(count < totalCount) {
	 						li.append(' :: ');
	 					}
	 					count++;
 				});
 				totalAdded++;
 				$(this).data('totalAdded', totalAdded);
					$(relation).find('.multiselect-search').each(function() { 
						$(this).data('value', '')
						$(this).val('');
					});
 				var removeButton = $('<a/>').html('X').addClass('remove-multiselect');
 				removeButton.click(function(e) {
 					$(this).parent('li').fadeOut(function() {
 						$(this).remove();
 					});
 				});
 				removeButton.appendTo(li);
 				li.appendTo($(relation).find('.triangular-relation-list'));
 				relation.find('input[type="text"]').attr('disabled', false);
 				$(this).attr('disabled', 'disabled');
 			});
 			var token = $('#concerto-token').val();
 			$(this).find('.content-relation').each(function() {
 				var name = $(this).attr('name');
 				$(this).attr('name', '');
 				$(this).hide();
 				
 				
 				
 				var select = this;
	 			var contentType = $(this).attr('data-content-type');
	 			var search = $('<input type="text"/>').attr('class', 'multiselect-search').data('content-type', contentType);
	 			var results = $('<ul/>').attr('class', 'multiselect-search-results').css('display', 'none');
				var clearBoth = $('<div/>').css('clear', 'both');
				var createStub = $('<button/>').html('+').attr('disabled', 'disabled').addClass('multiselect-create-stub').addClass('small-button');
				$(this).after(search);
				search.after(createStub);
				createStub.click(function(e) {
					$(this).attr("disabled", 'disabled');
					search.attr("disabled", 'disabled');
					e.preventDefault();
					var val = search.val();
					var rootUrl = concerto.rootUrl;
					$.ajax({
						url:rootUrl + '?view=api&display=manipulate',
						dataType:'json',
						data:{
							'object':'content',
							'content_type':contentType,
							'content_title':val,
							'content_status':'stub',
							'concerto_token':token
						},
						type:'POST',
						error: function(a,b,c) {
							alert('error: '+c);
						},
						
						success:function(data) {
							var option = $('<option/>').attr('value', data.id).attr('selected', 'selected').html(val).appendTo(select);
							var append = $('<li/>').data('id', data.id).data('name', val).html(val).appendTo(results).click(function() {
								search.data('value', $(this).data('id'));
								search.val($(this).data('name'));
								results.hide();
								addButton.checkValidity();
							});
							
							search.attr('disabled', false);
							
						}
					});
				});
				createStub.after(clearBoth);
				
				clearBoth.after(results);
				search.attr('placeholder', 'Search...');
				results.css('width', search.width() + 'px');
				
				
					
				search.keyup(function() {
					results.html('');
					var query = $(this).val();
					if(query) {
						createStub.attr('disabled', 'disabled');
						results.show();
						$(select).find(":contains('" + query + "')").each(function() {
							var child = $('<li/>').html($(this).html()).appendTo(results);
							child.data('option', this);
							child.click(function() {
								search.data('value', $($(this).data('option')).val());
								search.val($($(this).data('option')).html());
								results.hide();
								addButton.checkValidity();
							});
						});
						if(!results.children('li').length && !$(select).find(":contains('" + query + "')").length) {
							createStub.attr('disabled', false);
						}
					}
					else {
						createStub.attr('disabled', 'disabled');
						results.hide();
						search.data('value', '');
						
					}
				
				});
 			
 			
 			})
 			
		});
	};
})(jQuery);